package cn.ujn.medicalbackend.controller;

import cn.ujn.medicalbackend.common.BaseResponse;
import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.common.ResultUtils;
import cn.ujn.medicalbackend.domain.Drugcompany;
import cn.ujn.medicalbackend.dto.DrugcompanyQuery;
import cn.ujn.medicalbackend.dto.DrugcompanyQuery;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.*;
import cn.ujn.medicalbackend.service.DrugcompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/drugcompany")
@CrossOrigin(origins = {"http://localhost:9092"})
public class DrugcompanyController {

    @Autowired
    private DrugcompanyService drugcompanyService;

    @GetMapping("/list/drugcompany")
    public BaseResponse<Page<Drugcompany>> getdrugcompanyListByPage(DrugcompanyQuery drugcompanyQuery) {
        if (drugcompanyQuery== null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Drugcompany drugcompany = new Drugcompany();
        BeanUtils.copyProperties(drugcompanyQuery, drugcompany);
        QueryWrapper<Drugcompany> queryWrapper = new QueryWrapper<>(drugcompany);
        Page<Drugcompany> page = new Page<>(drugcompanyQuery.getPageNum(), drugcompanyQuery.getPageSize());
        Page<Drugcompany> tempPage = drugcompanyService.page(page, queryWrapper);
        Page<Drugcompany> resultPage = drugcompanyService.DrugcompanyPageTransfer(tempPage);
        return ResultUtils.success(resultPage);
    }

    @PostMapping("/add")
    public BaseResponse<Long> addDrugcompany(@RequestBody DrugcompanyAddRequest drugcompanyAddRequest, HttpServletRequest request) {
        //todo 添加
        if (drugcompanyAddRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long drugcompanyId = drugcompanyService.addDrugcompany(drugcompanyAddRequest, request);
        return ResultUtils.success(drugcompanyId );
    }

    @PostMapping("/update")
    private BaseResponse<Boolean> updateDrugcompany(@RequestBody DrugcompanyUpdateRequest drugcompanyUpdateRequest, HttpServletRequest request) {

        if (drugcompanyUpdateRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean result = drugcompanyService.updateDrugcompany(drugcompanyUpdateRequest,request);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    private BaseResponse<Boolean> deleteDrugcompany(@RequestBody DrugcompanyDeleteRequest drugcompanyDeleteRequest, HttpServletRequest request) {
        if (drugcompanyDeleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result =  drugcompanyService.deleteDrugcompany(drugcompanyDeleteRequest, request);
        return ResultUtils.success(result);
    }




}
